function randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; 
    var maxPos = $chars.length;
    var ranstr = '';
    for (i = 0; i < len; i++) {
        ranstr += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return ranstr;
}

layui.use(['form'], function() {
    var form = layui.form;
    
    $("input[name='username']").focus();
    $('#vcImg').attr('src','validatecode?randomStr='+randomString(6));
    $('.sign-title div.codelogin').on('click', function(){
        $(this).children().addClass('active');
        $(this).siblings().children().removeClass('active');
        $('.sign-body div.codelogin').removeClass('layui-hide');
        $('.sign-body div.qrlogin').addClass('layui-hide');
        $('.qrlogin .qrcode-img').empty();
        clearInterval(e); 
    });
    
    $('div.beg-remember').on('click', function(){$('.dropdown-menu').toggle();});
    $('div.layui-form-item').on('click', 'i.layui-extend-icon_eye_off', function(){$(this).prev().attr('type','text');$(this).removeClass('layui-extend-icon_eye_off').addClass('layui-extend-icon_eye');});
    $('div.layui-form-item').on('click', 'i.layui-extend-icon_eye', function(){$(this).prev().attr('type','password');$(this).removeClass('layui-extend-icon_eye').addClass('layui-extend-icon_eye_off');});
   
    $(document).keydown(function (e) {
        var event = document.all ? window.event : e;
        if (event.keyCode === 13) {
            $('.layui-btn.sign-btn').click();
        }
    });
    
    form.verify({
          username: function(value, item){ 
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
              return '用户登录名不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
              return '用户登录名首尾不能出现下划线\'_\'';
            }
          }
     });
    
    form.on('submit(codelogin)',function(data){
        var btnObj = $(this);
        btnObj.val("正在登录...");
        btnObj.prop('disabled','disabled');
        var params = {};
        var jsEncrypt = new JSEncrypt();
        jsEncrypt.setPublicKey($('#pubkey').val());
        params.username=$('#username').val();
        params.validateCode=$('#validateCode').val();
        params.password=$('#pubword').val();
        params.password=encodeURI(jsEncrypt.encrypt(params.password)).replace(/\+/g, '%2B');
        params._csrf=$('#_csrf').val();
        $.ajax({
            url : 'loginProcesses',
            type : 'POST',
            cache: false,
            dataType:'json',
            data : params,
            success: function(data) {
                if(data!=null){
                    if(data.code == 0){
                        window.location.href = data.result.redirectUrl;
                    }else{
                        btnObj.val("登录");
                        btnObj.removeAttr('disabled');
                        $('.sign-tip').text(data.msg);
                        $('#vcImg').attr('src','validatecode?randomStr='+randomString(6));
                    }
                }
            },
            error: function(XmlHttpRequest, textStatus, errorThrown){
                if(XmlHttpRequest.status == 403){
                    alert("对不起，当前会话已过期，请刷新页面试试！", {icon:2});
                }else if(XmlHttpRequest.status == 500){
                    alert("请求出错啦，请稍后再试试看！", {icon:2});
                }else{
                    alert(XmlHttpRequest.responseText, {icon:2});
                }
                btnObj.val("登录");
                btnObj.removeAttr('disabled');
            }
        });
        return false;
    });
})
